<?php
include_once 'BaseS.php';
include_once $_SERVER ['DOCUMENT_ROOT'].BASE_PROJECT_PATH.'/application/model/MemberM.php';
include_once $_SERVER ['DOCUMENT_ROOT'].BASE_PROJECT_PATH.'/library/util/Util.php';

class MemberS extends BaseS {
	
	public function __construct() {
		parent::__construct ();
		parent::setTarget ( $this );
	}

	public function insert() {
		$obj = $this->getRequest("datas");
		$dao = new MemberM();
		$obj['date_create'] = Util::getNowDate("Y-m-d H:i:s");
		$result = $dao->insert($obj);
		if(isset($_SESSION['user'])){
			$this->logUserAction(Util::clientIp(), "會員", 1);
		}
		echo $result; 
	}
	
	public function update(){
		$obj = $this->getRequest("datas");
		if(isset($obj['birthday'])){
			$obj['birthday'] = $obj['birthday'] != 0 ? Util::getNowDate("Y") - $obj['birthday'] : 0 ;
		}
		$dao = new MemberM();
		$result = $dao->update($obj);
		if(isset($_SESSION['user'])){
			if(isset($obj['is_delete']) && $obj['is_delete'] == 1){
				$this->logUserAction(Util::clientIp(), "會員", 3);
			}else{
				$this->logUserAction(Util::clientIp(), "會員", 2);
			}
		}
		echo $result;
	}
	
	public function delete(){
		$id = $this->getRequest("id");
		$dao = new MemberM();
		$result = $dao->delete($id);
		if(isset($_SESSION['user'])){
			$this->logUserAction(Util::clientIp(), "會員", 3);
		}
		echo $result;
	}
	
	public function getTableData(){
		$key = $this->getRequest("dataId");;
		$dao = new MemberM();
		$result = $dao->getTableData($key);
		echo Util::toJson($result);
	}
	
	public function queryTable(){
		
		$result = array("sl"=>"1", "di"=>"0", "ti"=>"", "msg"=>"");
		$dao = new MemberM();
		$obj = $this->getRequest("datas");
		
		$startPage = ($obj['page']-1) * $obj['pageRow']; 
		$searchStr = "";
		
		if( $obj['searchData']['account'] != ""){$searchStr .= " and account like ('%".$obj["searchData"]["account"]."%') ";}
		if( $obj['searchData']['loginType'] != 0){$searchStr .= " and login_type = ".$obj["searchData"]["loginType"]." ";}
		if( $obj['searchData']['nickname'] != ""){$searchStr .= " and nickname like ('%".$obj["searchData"]["nickname"]."%') ";}
		if( $obj['searchData']['profession'] != 0){$searchStr .= " and profession_id = ".$obj["searchData"]["profession"]." ";}
		if( $obj['searchData']['sex'] != 0){$searchStr .= " and sex = ".$obj["searchData"]["sex"]." ";}

		$result['data'] = $dao->queryTable($obj['orderData'], $startPage, $obj['pageRow'], $searchStr);
		
		if($result["data"] == null){
			$result["di"] = "1";
			$result["ti"] = "系統異常";
			$result["msg"] = "資料庫無法連接！";
		}
		
		if($result["data"]['count'] == 0){
			$result["di"] = "1";
			$result["ti"] = "查無資料";
			$result["msg"] = "資料庫沒有相關資料！";
		}
		
		echo Util::toJson($result); 
	}

	/**
	 * 登入
	 */
	public function login() {
		
	    $account =  $this->getRequest("account");
    	//$password = $this->getRequest("password");
    	$password = Util::encrypt($this->getRequest("password"));
    	$dao = new MemberM();
    	
        $member = $dao->getMember($account, $password);	
    	
        if ($member == null) {
        	$dao->plusLoginFail($account);
        	$dao->insertLogMemberLoginout(array('member_account' => $account, 'ip' => Util::clientIp(), 'type' => 1, "is_login" => 0));
        	echo 2;
        	exit;
        } else {
        	$_SESSION["member"] = $member;
        	$liveKey = microtime(true) * 10000;
        	
        	$datas = array(
        			"id" => $member->id,
        			"live_key" => $liveKey,
        			"fail_times" => 0
        	);
        	
        	$user->live_key = $liveKey;
        	$dao->updateLiveKey($datas);
        	$_SESSION["member"] = $member;
        	
        	$dao->insertLogMemberLoginout(array('member_account' => $member->account, 'ip' => Util::clientIp(), 'type' => 1));
        	
        	echo 1;
        	exit;
        }
	}
	
	/**
	 * 登出
	 */
	public function logout() {
		
		$dao = new MemberM();
		$isClose = @$this->getRequest("isClose");
		$member = $_SESSION["member"] ;
		
        $dao->insertLogMemberLoginout(array('member_account' => $member->account, 'ip' => Util::clientIp(), 'type' => 2));
		unset($_SESSION["member"]);
		
		if (!$isClose) {
			Util::forward( BASE_PROJECT_PATH."index.php");
		}
	}
	
	
}

if (isset($_GET ["action"])) {
	$service = new MemberS ();
	$service->__call ( $_GET ["action"], null );
} 
?>